package sury.web.controller.back;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sury.biz.service.MenuService;
import sury.biz.service.RoleService;
import sury.common.result.Result;
import sury.dal.entity.AdminEntity;
import sury.dal.entity.RoleEntity;

import java.util.List;

/**
 * 后端接口-角色
 */
@RestController
@Slf4j
@RequestMapping("/back/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;

    /**
     * 分页查询角色
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/page")
    public Result pageAdmin(@RequestParam(value = "current",defaultValue = "1") Long current,
                            @RequestParam(value = "size",defaultValue = "10") Long size){
        Page<RoleEntity> page=new Page<>(current,size);
        return Result.success(roleService.page(page));
    }

    /**
     * 是否启用
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/updateStatus/{id}")
    public Result updateStatus(@PathVariable("id") Long id,@RequestParam("status") Integer status){
        boolean result=roleService.update(new UpdateWrapper<RoleEntity>().eq("id",id).set("status",status));
        return result?Result.success():Result.error("操作失败");//TODO:Srror msg constant
    }

    /**
     * 查看角色对应的菜单
     * @param id
     * @return
     */
    @GetMapping("/menus/{id}")
    public Result getRoleMenus(@PathVariable("id") Long id) {
        // 获取角色对应的所有菜单ID
        List<Long> menuIds = roleService.getMenuIdsByRoleId(id);

        // 根据菜单ID列表获取菜单详情
        return Result.success(menuService.listByIds(menuIds));
    }
}
